package com.zzyl.serve.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.dto.NursingLevelDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.vo.NursingLevelVo;
import com.zzyl.serve.vo.NursingPlanVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevelVo selectNursingLevelById(Long id) {
        NursingLevel nursingLevel = getById(id);
        NursingLevelVo nursingLevelVo = new NursingLevelVo();
        BeanUtils.copyProperties(nursingLevel, nursingLevelVo);
        return nursingLevelVo;
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        // 查询护理等级
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        for (NursingLevel level : nursingLevels) {
            //获取护理计划的id
            Long lplanId = level.getLplanId();
            //根据id查询护理计划
            NursingPlan nursingPlan = nursingPlanMapper.selectById(lplanId);
            level.setPlanName(nursingPlan.getPlanName());
        }

        return nursingLevels;
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevelDto 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        System.out.println(nursingLevel);
        int i = nursingLevelMapper.insertNursingLevel(nursingLevel);
        if(i > 0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int i = updateById(nursingLevel) ? 1 : 0;
        if((i) > 0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }
//
//    /**
//     * 批量删除护理等级
//     *
//     * @param ids 需要删除的护理等级主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingLevelByIds(Long[] ids)
//    {
//        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
//    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        NursingLevel nursingLevel = getById(id);
        if(nursingLevel.getStatus() ==1){
            return 0;
        }

        int i = removeById(id) ? 1 : 0;
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

    /**
     * 获取护理等级
     * @return
     */
    @Override
    public List<NursingLevel> getNursingLevelList() {
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper();
        list = list(queryWrapper.eq(NursingLevel::getStatus, 1));
        return list;
    }
}
